﻿


using Basket.API.Data;

namespace Basket.API.Baskets.GetBasket
{
    public record GetBasketQueryResult(ShoppingCart ShoppingCart);
    public record GetBasketQuery(string userName): IQuery<GetBasketQueryResult>;

    public class GetBasketQueryValidator : AbstractValidator<GetBasketQuery>
    {
        public GetBasketQueryValidator()
        {
            RuleFor(x => x.userName).NotEmpty().WithMessage("UserName is required");
        }
    }
    public class GetBasketQueryHandler(IBasketRepository repository) : IQueryHandler<GetBasketQuery, GetBasketQueryResult>
    {
        public async Task<GetBasketQueryResult> Handle(GetBasketQuery query, CancellationToken cancellationToken)
        {
            var cart=await repository.GetBasketAsync(query.userName);
            return new GetBasketQueryResult(cart);
        }
    }
}
